<script lang="ts" setup>
import type { EdubaseSchoolTeachingUnitApi } from '#/api/edu/base/schoolTeachingUnit';

import { ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';
import { IconifyIcon } from '@vben/icons';

import { Button } from 'ant-design-vue';

import { getSchoolTeachingUnit } from '#/api/edu/base/schoolTeachingUnit';
import { useDescription } from '#/components/description';

import { useDetailSchema } from '../data';

const formData = ref<EdubaseSchoolTeachingUnitApi.SchoolTeachingUnit>();

const [Descriptions] = useDescription({
  bordered: true,
  size: 'middle',
  column: 1,
  labelWidth: 160,

  schema: useDetailSchema(),
});
const desensitize = ref(true);
async function handleDesensitize() {
  desensitize.value = !desensitize.value;
  const data = await getSchoolTeachingUnit(
    formData.value.id,
    desensitize.value,
  );
  formData.value = data;
}
const [Modal, modalApi] = useVbenModal({
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    let data =
      modalApi.getData<EdubaseSchoolTeachingUnitApi.SchoolTeachingUnit>();
    if (!data || !data.id) {
      return;
    }
    let orgInfoData = {
      showCampus: false,
      showCollege: false,
      showDepartment: false,
      showFaculty: false,
      showGroup: false,
      isSchool: false,
    };
    if (data.isSchool) {
      if (data.showCampus) {
        orgInfoData.campusDictList = data.campusDictList || [];
      }
      if (data.showCollege) {
        orgInfoData.collegeDictList = data.collegeDictList || [];
      }
      if (data.showDepartment) {
        orgInfoData.departmentDictList = data.departmentDictList || [];
      }
      if (data.showFaculty) {
        orgInfoData.facultyDictList = data.facultyDictList || [];
      }
      if (data.showGroup) {
        orgInfoData.groupDictList = data.groupDictList || [];
      }
      orgInfoData.isSchool = data.isSchool;
      orgInfoData.showCampus = data.showCampus;
      orgInfoData.showCollege = data.showCollege;
      orgInfoData.showDepartment = data.showDepartment;
      orgInfoData.showFaculty = data.showFaculty;
      orgInfoData.showGroup = data.showGroup;
      orgInfoData.distabledOrgType = data.distabledOrgType;
    } else {
      orgInfoData = {
        showCampus: false,
        showCollege: false,
        showDepartment: false,
        showFaculty: false,
        showGroup: false,
        isSchool: false,
      };
    }
    modalApi.lock();
    try {
      data = await getSchoolTeachingUnit(data.id, desensitize.value);
      formData.value = { ...data, ...orgInfoData };
    } finally {
      modalApi.unlock();
    }
  },
});
</script>

<template>
  <Modal
    title="查看教学单位"
    class="w-[60%]"
    :show-cancel-button="false"
    :show-confirm-button="false"
  >
    <Descriptions :data="formData" class="mx-4" />
    <template #prepend-footer>
      <Button type="primary" @click="handleDesensitize" :danger="desensitize">
        <IconifyIcon
          :icon="
            desensitize
              ? 'ant-design:eye-filled'
              : 'ant-design:eye-invisible-filled'
          "
        />
        {{ desensitize ? '关闭脱敏' : '启用脱敏' }}
      </Button>
    </template>
  </Modal>
</template>
